/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.text.*;
import javax.swing.*;
import javax.swing.text.*;

import blueprint4j.utils.*;

public class DataPairFieldCharacter extends DataPairDefault implements FieldBinding {
    private BindCharacterInterface field;
    private JTextField text_field = null;
    
    public DataPairFieldCharacter(Binder binder,BindCharacterInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldCharacter(Binder binder,BindCharacterInterface p_field, boolean editable) {
        super(binder,p_field);
        text_field = new JTextField(1);
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        setComponent(new JScrollPane(text_field));
        text_field.setEditable(editable);
    }
    
    public boolean isChanged() {
        Character f = field.get();
        if (f == null) {
            return text_field.getText() == null || text_field.getText().length() == 0;
        } else {
            return !f.equals(new Character(text_field.getText().toCharArray()[0]));
        }
    }
    
    public void intoControl() {
        if (field.get() != null) {
            text_field.setText(field.get().toString());
        } else {
            text_field.setText("");
        }
    }
    
    public void intoBindable() {
        if (text_field.getText() != null && text_field.getText().length() > 0) {
            field.set(new Character(text_field.getText().toCharArray()[0]));
        } else {
            field.set(null);
        }
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindCharacterInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
}

